package com.mayikt.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mayikt.base.BaseApiService;
import com.mayikt.base.BaseResponse;
import com.mayikt.bean.MeiteBeanUtils;
import com.mayikt.dto.req.UnionLoginDTO;
import com.mayikt.entity.UnionLoginDO;
import com.mayikt.mapper.UnionLoginMapper;
import com.mayikt.service.MemberUnionLoginService;
import com.mayikt.strategy.UnionLoginStrategy;
import com.mayikt.utils.SpringContextUtils;
import com.mayikt.utils.TokenUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Name MemberUnionLoginServiceImpl
 * @Author 枫火
 * @Date 2021/5/28 7:10
 * @Description
 **/
@Controller
//@CrossOrigin
public class MemberUnionLoginServiceImpl extends BaseApiService implements MemberUnionLoginService {

    @Autowired
    private UnionLoginMapper unionLoginMapper;
    @Autowired
    private TokenUtil tokenUtil;
    @Value("${mayikt.login.vue.bindingurl}")
    private String bindingurl;

    @Override
    public BaseResponse<String> unionLogin(String unionPublicId) {
        if (StringUtils.isEmpty(unionPublicId)) {
            return setResultError("unionPublicId不能为空");
        }
        // 根据渠道id查询联合登录基本信息
        UnionLoginDO unionLoginDo = unionLoginMapper.selectByUnionLoginId(unionPublicId);
        if (unionLoginDo == null) {
            return setResultError("该渠道可能已经关闭或者不存在");
        }
        // state 防止重复提交
        String state = tokenUtil.createToken("member.unionLogin", "");
        String requestAddress = unionLoginDo.getRequestAddress() + "&state=" + state;
        JSONObject dataObjects = new JSONObject();
        dataObjects.put("requestAddress", requestAddress);
        return setResultSuccess(dataObjects);
    }

//    @Override
//    public BaseResponse<JSONObject> unionLoginCallback(String unionPublicId) {
//        if (StringUtils.isEmpty(unionPublicId)) {
//            return setResultError("unionPublicId不能为空");
//        }
//        // 根据渠道id查询 联合基本信息
//        UnionLoginDO unionLoginDo = unionLoginMapper.selectByUnionLoginId(unionPublicId);
//        if (unionLoginDo == null) {
//            return setResultError("该渠道可能已经关闭或者不存在");
//        }
//        String unionBeanId = unionLoginDo.getUnionBeanId();
//        if (StringUtils.isEmpty(unionBeanId)) {
//            return setResultError("系统参数错误");
//        }
//        //  从Spring容器中根据beanId 查找到策略类
//        UnionLoginStrategy unionLoginStrategy = SpringContextUtils.getBean(unionBeanId, UnionLoginStrategy.class);
//        // 根据当前线程获取request对象
//        HttpServletRequest request = ((ServletRequestAttributes)
//                (RequestContextHolder.currentRequestAttributes())).getRequest();
//        String openId = unionLoginStrategy.unionLoginCallback(request, unionLoginDo);
//        if (StringUtils.isEmpty(openId)) {
//            return setResultError("系统错误");
//        }
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("openId", openId);
//        jsonObject.put("unionPublicId", unionPublicId);
//        String openToken = tokenUtil.createToken("mayikt.unionLogin.", jsonObject.toJSONString());
//        JSONObject dataToken = new JSONObject();
//        dataToken.put("openToken", openToken);
//        return setResultSuccess(dataToken);
//    }

    @Override
    public String unionLoginCallback(String unionPublicId) {
        // 根据渠道id查询 联合基本信息
        UnionLoginDO unionLoginDo = unionLoginMapper.selectByUnionLoginId(unionPublicId);
        String unionBeanId = unionLoginDo.getUnionBeanId();
        //  从Spring容器中根据beanid 查找到我们的策略类
        UnionLoginStrategy unionLoginStrategy = SpringContextUtils.getBean(unionBeanId, UnionLoginStrategy.class);
        // 根据当前线程获取request对象
        HttpServletRequest request = ((ServletRequestAttributes)
                (RequestContextHolder.currentRequestAttributes())).getRequest();
        String openId = unionLoginStrategy.unionLoginCallback(request, unionLoginDo);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("openId", openId);
        jsonObject.put("unionPublicId", unionPublicId);
        String openToken = tokenUtil.createToken("mayikt.unionLogin.", jsonObject.toJSONString());
        return "redirect:" + bindingurl + openToken;
    }

    @Override
    public BaseResponse<List<UnionLoginDTO>> unionLoginList() {
        List<UnionLoginDO> unionLoginList = unionLoginMapper.selectByUnionLoginList();
        if (unionLoginList == null) {
            return setResultError("当前没有可用渠道");
        }
        List<UnionLoginDO> unionLoginDtos = MeiteBeanUtils.doToDtoList(unionLoginList, UnionLoginDO.class);
        return setResultSuccess(unionLoginDtos);
    }
}

